<template>
  <div class="chronic-disease-type-management">
    <!-- 搜索区域 -->
    <div class="operation-container">
      <el-form :inline="true" :model="queryParams">
        <el-form-item label="种类名称">
          <el-input
            v-model="queryParams.typeName"
            placeholder="请输入种类名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            v-permission="'disease:type:query'"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="button-group">
        <el-button
          type="primary"
          v-permission="'disease:type:add'"
          @click="handleAdd"
        >
          <el-icon><Plus /></el-icon>新增慢性病种类
        </el-button>
      </div>
    </div>

    <!-- 表格 -->
    <el-table
      :data="diseaseTypeList"
      border
      stripe
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="typeName" label="种类名称" width="200" />
      <el-table-column prop="description" label="描述" width="300" />
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="220">
        <template #default="{ row }">
          <el-button
            v-permission="'disease:type:edit'"
            size="small"
            @click="handleEdit(row)"
            >编辑</el-button
          >
          <el-button
            v-permission="'disease:type:delete'"
            size="small"
            type="danger"
            icon="Delete"
            plain
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
      <el-form
        ref="diseaseTypeForm"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="种类名称" prop="typeName">
          <el-input v-model="formData.typeName" placeholder="请输入种类名称" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="formData.description"
            type="textarea"
            :rows="3"
            placeholder="请输入描述"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getChronicDiseaseTypeList,
  addChronicDiseaseType,
  updateChronicDiseaseType,
  deleteChronicDiseaseType
} from '@/api/chronicDiseaseType'

// 获取用户权限

// 查询参数
const queryParams = reactive({
  page: 1,
  pageSize: 10,
  typeName: ''
})

// 表格数据
const diseaseTypeList = ref([])
const total = ref(0)
const loading = ref(false)

// 对话框控制
const dialogVisible = ref(false)
const dialogTitle = ref('')
const isAdd = ref(true)

// 表单数据
const formData = reactive({
  id: null,
  typeName: '',
  description: ''
})

// 表单验证规则
const rules = reactive({
  typeName: [
    { required: true, message: '请输入种类名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  description: [
    { max: 500, message: '描述长度不能超过500个字符', trigger: 'blur' }
  ]
})

// 获取慢性病种类列表
const fetchDiseaseTypeList = async () => {
  try {
    loading.value = true
    const res = await getChronicDiseaseTypeList(queryParams)
    diseaseTypeList.value = res.data.data.records
    total.value = res.data.data.total
  } finally {
    loading.value = false
  }
}

// 搜索
const handleQuery = () => {
  queryParams.page = 1
  fetchDiseaseTypeList()
}

// 重置搜索
const resetQuery = () => {
  queryParams.typeName = ''
  handleQuery()
}

// 分页大小变化
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  fetchDiseaseTypeList()
}

// 当前页变化
const handleCurrentChange = (val) => {
  queryParams.page = val
  fetchDiseaseTypeList()
}

// 新增慢性病种类
const handleAdd = () => {
  dialogTitle.value = '新增慢性病种类'
  isAdd.value = true
  resetForm()
  dialogVisible.value = true
}

// 编辑慢性病种类
const handleEdit = (row) => {
  dialogTitle.value = '编辑慢性病种类'
  isAdd.value = false
  Object.assign(formData, row)
  dialogVisible.value = true
}

// 删除慢性病种类
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该慢性病种类吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    center: true
  })
    .then(async () => {
      await deleteChronicDiseaseType(row.id)
      ElMessage.success('删除成功')
      fetchDiseaseTypeList()
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    id: null,
    typeName: '',
    description: ''
  })
}

// 提交表单
const submitForm = async () => {
  try {
    if (isAdd.value) {
      await addChronicDiseaseType(formData)
      ElMessage.success('新增成功')
    } else {
      await updateChronicDiseaseType(formData)
      ElMessage.success('更新成功')
    }
    dialogVisible.value = false
    fetchDiseaseTypeList()
  } catch (error) {
    console.error(error)
  }
}

// 初始化加载数据
onMounted(() => {
  fetchDiseaseTypeList()
})
</script>

<style scoped>
.chronic-disease-type-management {
  padding: 20px;
}

.operation-container {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.button-group {
  margin-left: 10px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>
